package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.atguigu.gulimall.product.vo.AttrResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author biubiu0002
 * @email 1007918011@qq.com
 * @date 2021-05-23 12:14:36
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("categoryId") Long categoryId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();

        Long[] categoryPath = categoryService.findCategoryPath(catelogId);
        attrGroup.setCatelogPath(categoryPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @GetMapping("/{groupId}/attr/relation")
    public R attrRelationList(@PathVariable("groupId")Long groupId){
        List<AttrEntity> list =  attrService.getRelationAttr(groupId);
        return R.ok().put("data",list);

    }

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOs){
        attrService.saveRelation(attrGroupRelationVOs);
        return R.ok();

    }

    //product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOs){
        attrService.deleteRelation(attrGroupRelationVOs);
        return R.ok();

    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String,Object>params,
                            @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page =  attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page",page);
    }

    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable("catelogId") Long catalogId){
        List<AttrGroupWithAttrsVO> list = attrService.withAttr(catalogId);
        return R.ok().put("data",list);

    }


}
